package top.tangyh.lamp.care.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.tangyh.basic.base.R;
import top.tangyh.basic.exception.BizException;
import top.tangyh.lamp.care.entity.BGenItem;
import top.tangyh.lamp.care.service.BGenItemService;
import top.tangyh.lamp.care.vo.query.GenItemQueryForRdParam;
import top.tangyh.lamp.care.vo.query.GenItemQueryParam;
import top.tangyh.lamp.care.vo.result.GenItemDto;
import top.tangyh.lamp.care.vo.result.PageOutputDTO;
import top.tangyh.lamp.care.vo.save.GenItemQuickDeleteParam;
import top.tangyh.lamp.care.vo.save.GenItemQuickSaveParam;
import top.tangyh.lamp.care.vo.save.GenItemSaveParam;
import top.tangyh.lamp.care.vo.save.ManulGenItemDto;
import top.tangyh.lamp.care.vo.update.ManulGenItemUpdateDto;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * 通用项目配置表
 * </p>
 *
 * @author zqb
 * @date 2024-07-17 17:16:35
 * @create [2024-07-17 17:16:35] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/bGenItem")
@Tag(name = "通用项目配置")
public class BGenItemController  {
//    extends SuperController<BGenItemService, Long, BGenItem
//            , BGenItemSaveVO, BGenItemUpdateVO, BGenItemPageQuery, BGenItemResultVO>
//    private final EchoService echoService;
//    @Override
//    public EchoService getEchoService() {
//        return echoService;
//    }

    private final BGenItemService bGenItemService;

    @Operation(summary ="分页查询通用项目")
    @PostMapping("/list")
    public R<PageOutputDTO<GenItemDto>> queryPage(@Valid @RequestBody GenItemQueryParam param) {
        return R.success(bGenItemService.queryPage(param));
    }

    @Operation(summary ="查询通用项目(不分页）")
    @PostMapping("/listAll")
    public R<List<BGenItem>> queryAll(@Valid @RequestBody GenItemQueryParam param) {
        return R.success(bGenItemService.queryAll(param));
    }


    @Operation(summary ="保存通用项目")
    @PostMapping("/save")
    public R<GenItemDto> create(@Valid @RequestBody GenItemSaveParam param) {
        return R.success(bGenItemService.insertOrUpdate(param, true));
    }

    @Operation(summary ="出入量那边手动添加选项")
    @PostMapping("/manual/save")
    public R<Long> manualCreate(@Valid @RequestBody ManulGenItemDto param) {
        return R.success(bGenItemService.manualInsertByInOutModule(param));
    }

    @Operation(summary ="删除出入量手动添加的选项")
    @DeleteMapping("/manual/delete")
    public R manualDelete(Long id) {
        BGenItem yxGenItem=bGenItemService.getById(id);
        if(null!=yxGenItem){
            if(yxGenItem.getIsManual()==0){
                throw new BizException("不能删除非手动添加的选项");
            }
        }else{
            throw new BizException("该选项已经不存在，请刷新页面再次确认");
        }
        List<Long> ids=new ArrayList<>();
        ids.add(id);
        bGenItemService.removeByIds(ids);
        return R.success();
    }

    @Operation(summary ="出入量那边手动添加选项修改名称")
    @PutMapping("/manual/update/name")
    public R<BGenItem> manualUpdateName(@Valid @RequestBody ManulGenItemUpdateDto param) {
        BGenItem update=new BGenItem();
        update.setId(param.getId());
        update.setItemName(param.getName());
        return R.success(bGenItemService.updateById(update));
    }

    @Operation(summary ="查询通用项目配置（应用于对应记录业务模块）")
    @PostMapping("/listForRd")
    public R<LinkedHashMap<String, BGenItem>> queryAllForRd(@Valid @RequestBody GenItemQueryForRdParam param) {
        return R.success(bGenItemService.queryAllByPid(param.getModelCode(), param.getPatientIdent()));
    }

    @Operation(summary ="保存通用项目配置（应用于对应记录业务模块）")
    @PostMapping("/savewRd")
    public R<String> queryAllForRd(@Valid @RequestBody List<GenItemSaveParam> param) {
        bGenItemService.insertOrUpdate(param, false);
        return R.success("success");
    }

    @Operation(summary ="删除通用项")
    @DeleteMapping("/delete")
    public R delete(@RequestParam("ids") List<Long> ids) {
        bGenItemService.removeByIds(ids);
        return R.success();
    }

    @PostMapping("/import")
    public R<Boolean> importExcel(@RequestParam("file") MultipartFile file) {
        bGenItemService.importExcel(file);
        return R.success();
    }


    @PutMapping("/quickcreate")
    public R quickcreate(@Valid @RequestBody GenItemQuickSaveParam param) {
        bGenItemService.quickcreate(param);
        return R.success();
    }


    @PostMapping("/quickdelete")
    public R quickdelete(@Valid @RequestBody GenItemQuickDeleteParam param) {
        bGenItemService.quickdelete(param);
        return R.success();
    }

}


